package ai.people.core.common.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;

import javax.servlet.http.HttpServletRequest;
import java.net.InetSocketAddress;

/**
 * ip地址工具类
 *
 * @author zhenglin
 * @date 2022/04/07
 */
public class IpAddressUtil {

    private static final String LOCAL_IP = "127.0.0.1";

    /**
     * 获得ip地址
     *
     * @param request 请求
     * @return {@link String}
     */
    public static String getIpAddress(HttpServletRequest request) {
        String xip = request.getHeader("X-Real-IP");
        String xFor = request.getHeader("X-Forwarded-For");
        if (StringUtils.isNotEmpty(xFor) && !"unKnown".equalsIgnoreCase(xFor)) {
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = xFor.indexOf(",");
            if (index != -1) {
                return checkLocal(xFor.substring(0, index));
            } else {
                return checkLocal(xFor);
            }
        }
        xFor = xip;
        if (StringUtils.isNotEmpty(xFor) && !"unKnown".equalsIgnoreCase(xFor)) {
            return xFor;
        }
        if (StringUtils.isBlank(xFor) || "unknown".equalsIgnoreCase(xFor)) {
            xFor = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(xFor) || "unknown".equalsIgnoreCase(xFor)) {
            xFor = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(xFor) || "unknown".equalsIgnoreCase(xFor)) {
            xFor = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isBlank(xFor) || "unknown".equalsIgnoreCase(xFor)) {
            xFor = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isBlank(xFor) || "unknown".equalsIgnoreCase(xFor)) {
            xFor = request.getRemoteAddr();
        }
        return checkLocal(xFor);
    }

    public static String getIpAddress(ServerHttpRequest request) {
        HttpHeaders headers = request.getHeaders();
        String gateWayFordIp = headers.get("origin-ip") == null ? null : headers.get("origin-ip").get(0);
        if (StringUtils.isNotBlank(gateWayFordIp)) {
            return checkLocal(gateWayFordIp);
        }
        String xip = headers.get("X-Real-IP") == null ? null : headers.get("X-Real-IP").get(0);
        String xFor = headers.get("X-Forwarded-For") == null ? null : headers.get("X-Forwarded-For").get(0);
        if (StringUtils.isNotEmpty(xFor) && !"unKnown".equalsIgnoreCase(xFor)) {
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = xFor.indexOf(",");
            if (index != -1) {
                return checkLocal(xFor.substring(0, index));
            } else {
                return checkLocal(xFor);
            }
        }
        xFor = xip;
        if (StringUtils.isNotEmpty(xFor) && !"unKnown".equalsIgnoreCase(xFor)) {
            return xFor;
        }
        if (StringUtils.isBlank(xFor) || "unknown".equalsIgnoreCase(xFor)) {
            xFor = headers.get("Proxy-Client-IP") == null ? null : headers.get("Proxy-Client-IP").get(0);
        }
        if (StringUtils.isBlank(xFor) || "unknown".equalsIgnoreCase(xFor)) {
            xFor = headers.get("WL-Proxy-Client-IP") == null ? null : headers.get("WL-Proxy-Client-IP").get(0);
        }
        if (StringUtils.isBlank(xFor) || "unknown".equalsIgnoreCase(xFor)) {
            xFor = headers.get("HTTP_CLIENT_IP") == null ? null : headers.get("HTTP_CLIENT_IP").get(0);
        }
        if (StringUtils.isBlank(xFor) || "unknown".equalsIgnoreCase(xFor)) {
            xFor = headers.get("HTTP_X_FORWARDED_FOR") == null ? null : headers.get("HTTP_X_FORWARDED_FOR").get(0);
        }
        if (StringUtils.isBlank(xFor) || "unknown".equalsIgnoreCase(xFor)) {
            InetSocketAddress remoteAddress = request.getRemoteAddress();
            xFor = remoteAddress.getHostString();
        }
        return checkLocal(xFor);
    }

    private static String checkLocal(String ip){
        return "0:0:0:0:0:0:0:1".equals(ip) ? LOCAL_IP : ip;
    }
}
